<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目" prop="projectId">
        <el-select v-model="selectValue" placeholder="请选择项目" @change="getList">
          <el-option
            v-for="item in projectList"
            :key="item.projectId"
            :label="item.projectName"
            :value="item.projectId"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <el-tree :data="treeData" :props="treeProps" default-expand-all :expand-on-click-node="false" style="display: inline-block">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span style="margin-left: 100px">
          <el-button v-if="data.level !== 3"
                     type="text" size="mini"
                     @click="() => handleAdd(node, data)">添加</el-button>
          <el-button v-if="data.level !== 0"
                     type="text" size="mini"
                     @click="() => handleUpdate(node, data)">修改</el-button>
          <el-button v-if="data.level !== 0"
                     type="text" size="mini"
                     @click="() => handleDelete(node, data)">删除</el-button>
<!--          v-if="!(node.level === 1 || node.level === 2 || node.level === 3)"-->
        </span>
      </span>
    </el-tree>

    <!-- 添加或修改工程复盘目录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="目录名称" prop="recoveryTitle">
          <el-input v-model="form.recoveryTitle" placeholder="请输入工程复盘标题" />
        </el-form-item>
        <el-form-item label="排序" prop="recoverySort">
          <el-input-number v-model="form.recoverySort"/>
        </el-form-item>
        <el-form-item label="项目" prop="projectId" v-show="showFormItem">
          <span v-text="form.projectName">项目</span>
        </el-form-item>
        <el-form-item label="上级目录" prop="recoveryPid" v-show="showFormItem">
          <span>{{ form.parentName }}</span>
        </el-form-item>
        <el-form-item label="级别" prop="level" v-show="showFormItem">
          <span>{{ form.level }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTreeList, getRecovery, delRecovery, addRecovery, updateRecovery } from "@/api/project/recovery";
import {listProject} from "@/api/project/project";
export default {
  name: "Recovery",
  data() {
    return {
      showFormItem:true,
      // 下拉绑定值
      selectValue: null,
      treeData: [],
      treeProps: {
        label: 'name',
        children: 'children'
      },
      // 项目列表
      projectList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程复盘目录表格数据
      recoveryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        projectName:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        recoveryTitle: [
          { required: true, message: "目录名称不能为空", trigger: "blur" },
          { min: 1, max: 15, message: "目录名称长度必须介于 1 和 15 之间", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getProjectList();
  },
  methods: {
    /** 查询项目列表 */
    getProjectList() {
      listProject().then(resp => {
        this.projectList = resp.rows;
      })
    },
    /** 查询工程复盘目录列表 */
    getList() {
      getTreeList(this.selectValue).then((resp)=>{
        this.treeData = [resp.data];
      }).catch(()=>{})
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recoveryId: null,
        projectId: null,
        projectName: null,
        recoveryTitle: null,
        recoverySort: null,
        recoveryPid: null,
        level: null,
        parentName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recoveryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(node,data) {
      this.reset();
      this.open = true;
      this.title = "添加工程复盘目录";
      this.form.projectId = this.selectValue;
      this.form.projectName = this.projectList.find(item => item.projectId === this.selectValue).projectName;
      this.form.recoveryPid = data.id;
      this.form.parentName = node.label;
      this.form.level = node.level;
    },
    /** 修改按钮操作 */
    handleUpdate(node,data) {
      this.reset();
      this.open = true;
      this.title = "修改工程复盘目录";
      getRecovery(data.id).then(response => {
        this.form = response.data;
        this.form.parentName = node.parent.label;
        this.form.projectId = this.selectValue;
        this.form.projectName = this.projectList.find(item => item.projectId === this.selectValue).projectName;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recoveryId != null) {
            updateRecovery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecovery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(node,data) {
      this.$alert(data.id)
      this.$modal.confirm("确认删除吗?")
        .then(()=>{
          delRecovery(data.id).then((r)=>{
            this.$modal.msgSuccess("删除成功");
            this.getList();
          }).catch(()=>{});
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/recovery/export', {
        ...this.queryParams
      }, `recovery_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
